package com.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.base.BaseAction;
import com.entity.Vote;
import com.entity.VoteUser;
import com.service.VoteService;
import com.util.Page;

@SuppressWarnings("serial")
public class VoteAction extends BaseAction {

	public String test(){
		msg ="测试";
		return "success";
	}
	public String test1(){
		System.out.println(msg);
		return "success";
	}
	

	List<Vote> list;
	Vote e;
	Integer id;
	@Resource(name = "voteservice")
	VoteService service;
	Page p = new Page();
	Integer vtid;
	public String main() {
		getAllVoteType();
		p = service.getPage(new Vote(), p, "");
		return "main";
	}
	
	public String usermain(){
		int uid = ((VoteUser)getSession().getAttribute("user")).getVuid();
		p = service.getVoteExcludeCurrUser(uid,p);
		return "main";
	}
	
	public String getAllByType(){
		p = service.getVoteByType(vtid, p);
		return "classify";
	}

	public String delete() {
		try {
			service.delete(id);
			msg = "删除成功!";
		} catch (Exception e) {
			e.printStackTrace();
			msg ="删除失败，服务器内部错误！";
			return "fail";
		}
		return "delsuccess";
	}

	public String update() {
		try {
			e.setVcreatetime(new Date());
			service.update(e);
			msg = "更新成功！";
		} catch (Exception e) {
			e.printStackTrace();
			msg ="更新失败，服务器内部错误！";
			return "fail";
		}
		return "updatesuccess";
	}

	public String get() {
		e = service.get(id);
		if(e == null){
			msg = "查询出错，请重试！";
			return "fail";
		}
		return "votejson";
	}

	public String edit() {
		try {
			e = service.get(id);
			if(e == null){
				msg = "修改的数据不存在，请重新操作！";
				return "fail";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg ="服务器内部出错！";
			return "fail";
		}
		return "edit";
	}

	public String save() {
		try {
			e.setVcreatetime(new Date());
			e.setVturnout(0);
			service.save(e);
			msg = "新增成功！";
		} catch (Exception e) {
			e.printStackTrace();
			msg ="新增失败，服务器内部出错！";
			return "fail";
		}
		return "addsuccess";
	}
	
	public String add() {
		return "add";
	}
	
	public List<Vote> getList() {
		return list;
	}

	public void setList(List<Vote> list) {
		this.list = list;
	}

	public Vote getE() {
		return e;
	}

	public void setE(Vote e) {
		this.e = e;
	}

	public Integer getId() {
		return id;
	}

	public Integer getVtid() {
		return vtid;
	}

	public void setVtid(Integer vtid) {
		this.vtid = vtid;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public Page getP() {
		return p;
	}

	public void setP(Page p) {
		this.p = p;
	}

}
